package com.koron.device.service.propconfig;

import com.koron.device.bean.Equipment;
import com.koron.device.bean.EquipmentQuery;
import com.koron.device.mapper.EquipmentMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author twcao
 * @title: ChildrenNumConfig
 * @projectName GDH_EAM_V2
 * @description: 子节点属性配置
 * @date 2022/2/1514:47
 */
@Component
public class ChildrenNumConfig implements PropConfig {

    @Override
    public List<String> sign() {
        return Arrays.asList("hasChildren", "childrenNum");
    }

    @Override
    public void setup(SessionFactory factory, List<Equipment> equipments, EquipmentQuery query) {
        EquipmentMapper mapper = factory.getMapper(EquipmentMapper.class);
        // 批量查询子节点数量
        List<Map> childrenNum = mapper.countChildren(equipments.stream().map(Equipment::getId).collect(Collectors.toList()));
        Map<String, Long> childrenMap = childrenNum.stream().collect(Collectors.toMap(map -> (String)map.get("parentId"), map -> (long)map.get("num")));
        equipments.stream().filter(row -> childrenMap.containsKey(row.getId())).forEach(equipment -> equipment.setChildrenNum(childrenMap.get(equipment.getId()).intValue()));
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
